package com.cloudloan.controller;

import com.cloudloan.common.R;
import com.cloudloan.entity.User;
import com.cloudloan.mapper.LoginMapper;
import com.cloudloan.service.LoginServcie;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLIntegrityConstraintViolationException;

@RestController
@RequestMapping("/user")
@Api(value = "用户服务",tags = "用户服务")
public class LoginController {

    @Autowired
    private LoginServcie loginServcie;
    @Autowired
    private LoginMapper loginMapper;

    /**
     * 登陆接口
     * @param user
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("web端登录接口")
    public R login(@ModelAttribute User user){
        return loginServcie.login(user);
    }

    /**
     * 注册
     */
    @PostMapping("/sava")
    public R sava(@ModelAttribute User user){
        return loginServcie.sava(user);
    }
    /**
     * 查询
     */
    @GetMapping("/id")
    public R selectId(@RequestParam Long id){
        return loginServcie.selectId(id);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R updateById(@ModelAttribute User user){
        if (user.getId()==null){
            return R.error("为传递id");
        }
        return loginServcie.updateId(user);
    }

    /**
     * 退出接口
     * @return
     */
    @GetMapping("/logout")
    public R logout(){
        return loginServcie.logout();
    }
}
